<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 账户设置 -->
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              :model="formData"
              :rules="formRules"
              ref="form"
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input style="width: 300px" v-model="formData.username" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input
                  style="width: 300px"
                  type="password"
                  v-model="formData.password2"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <!-- 个人详情 -->
          <el-tab-pane label="个人详情">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <UserInfo @update="initData"></UserInfo>
          </el-tab-pane>

          <!-- 岗位信息 -->
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <JobInfo></JobInfo>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import UserInfo from './components/userInfo.vue' // 导入
import JobInfo from './components/jobInfo.vue' // 导入
import { getEmployeesInfoApi, editEmployeesApi } from '@/api/employees'
export default {
  name: 'Detail',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      // 传参id
      userId: this.$route.params.id,
      formData: {},
      formRules: {
        username: [{ required: true, message: '请输入', trigger: 'blur' }],
        password2: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getEmployeesInfoApi(this.$route.params.id)
      console.log(res)
      this.formData = res
    },
    async saveUser() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        console.log(error)
      }
      await editEmployeesApi({
        ...this.formData,
        password: this.formData.password2 //
      })
      this.$message.success('更新成功')
    }
  }
}
</script>

<style></style>
